package com.jms.tomcat.registration;

import com.jms.tomcat.config.JmsServletConfig;

import javax.servlet.*;
import java.util.*;

/**
 * servlet注册器
 */
public class JmsServletRegistration implements ServletRegistration.Dynamic {

    /**
     * servlet环境上下文
     */
    private final ServletContext servletContext;
    /**
     * servlet
     */
    private Servlet servlet;
    /**
     * servlet配置信息
     */
    private final ServletConfig servletConfig;
    /**
     * servlet名称
     */
    private final String name;
    /**
     * servlet初始化参数
     */
    private Map<String, String> initParameters = new HashMap<>();
    /**
     * servlet映射
     */
    private final List<String> urlPatterns = new ArrayList<>();
    /**
     * servlet是否初始化完成
     */
    private boolean isInitialized = false;

    public JmsServletRegistration(ServletContext servletContext, Servlet servlet, String name) {
        this.servletContext = servletContext;
        this.servlet = servlet;
        this.name = name;
        this.servletConfig = new JmsServletConfig(this);
    }

    public void setServlet(Servlet servlet) {
        this.servlet = servlet;
    }
    /**
     * 获取servlet上下文
     * @return ServletContext
     */
    public ServletContext getServletContext() {
        return this.servletContext;
    }

    /**
     * 获取servlet的config
     * @return ServletConfig
     */
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    public boolean isInitialized() {
        return isInitialized;
    }

    public void setInitialized(boolean initialized) {
        isInitialized = initialized;
    }

    public Servlet getServlet() {
        return servlet;
    }

    @Override
    public Set<String> addMapping(String... urlPatterns) {
        if (urlPatterns != null && urlPatterns.length > 0) {
            this.urlPatterns.addAll(Arrays.asList(urlPatterns));
        }
        return new HashSet<>(this.urlPatterns);
    }

    @Override
    public Collection<String> getMappings() {
        return urlPatterns;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getClassName() {
        return servlet.getClass().getName();
    }

    @Override
    public boolean setInitParameter(String name, String value) {
        boolean result = initParameters.containsKey(name);
        initParameters.put(name, value);
        return result;
    }

    @Override
    public String getInitParameter(String name) {
        return initParameters.get(name);
    }

    @Override
    public Set<String> setInitParameters(Map<String, String> initParameters) {
        this.initParameters = initParameters;
        return initParameters.keySet();
    }

    @Override
    public Map<String, String> getInitParameters() {
        return initParameters;
    }

    @Override
    public void setLoadOnStartup(int loadOnStartup) {
    }

    @Override
    public Set<String> setServletSecurity(ServletSecurityElement constraint) {
        return null;
    }

    @Override
    public void setMultipartConfig(MultipartConfigElement multipartConfig) {

    }

    @Override
    public void setRunAsRole(String roleName) {

    }

    @Override
    public void setAsyncSupported(boolean isAsyncSupported) {

    }

    @Override
    public String getRunAsRole() {
        return null;
    }
}
